package com.example.mybatisdemo.util;

import java.io.Serializable;

/**
 * API接口返回通用结果类
 * @author liuyj
 * @mail liuyingj@new-di.com
 * @datetime 2018-07-18 14:13
 * @param <T>
 */
public class CommonResult<T> implements Serializable {
	/**
	 * serialVersionUID:.
	 */
	private static final long serialVersionUID = -7268040542410707954L;

	/**
	 * 结果代码
	 */
	private int code;

	/**
	 * 返回信息
	 */
	private String message;
 
	/**
	 * 装在数据
	 */
	private T data;

	/**
	 * 错误代码
	 */
	private String description;
 
	/**
	 * 默认构造器
	 */
	public CommonResult(){
		
	}
	/**
	 * 
	 * @param success
	 * 			是否成功
	 * @param message
	 * 			返回的消息
	 */
	public CommonResult(boolean success, String message){
		if(success){
			this.code = CommonResultCode.SUCCESS.getCode();
		}else{
			this.code = CommonResultCode.FAIL.getCode();
		}
		this.message = message;
	}

	/**
	 * 
	 * @param success
	 * 			是否成功
	 */
	public CommonResult(boolean success){
		if(success){
			this.code = CommonResultCode.SUCCESS.getCode();;
		}else{
			this.code = CommonResultCode.FAIL.getCode();;
		}
	}

	/**
	 *
	 * @param code error code
	 * @param message success or error messages
	 */
	public CommonResult(int code,String message){
		this.code = code;
		this.message = message;
	}
	/**
	 * 
	 * @param success
	 * 			是否成功
	 * @param message
	 * 			消息
	 * @param data
	 * 			数据
	 */
	public CommonResult(boolean success, String message, T data){
		if(success){
			this.code = CommonResultCode.SUCCESS.getCode();
		}else{
			this.code = CommonResultCode.FAIL.getCode();
		}
		this.message = message;
		this.data = data;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}


	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setOk() {
		this.code = CommonResultCode.SUCCESS.getCode();
	}

	public void setFail() {
		this.code = CommonResultCode.FAIL.getCode();
	}

}
